// packageI/walletSiteBind/walletSiteBind.js
import {payKeyboardAsync} from "../../mycomponent/payKeyboard/PayKeyboardAsync";
let app = getApp();
Page({
  behaviors: [payKeyboardAsync],
  /**
   * 页面的初始数据
   */
  data: {
    popupSpecs: false,
    need_password: false,
    has_password: false,
    site: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    
  },

  getData(){
    app._postNetWork({
      url: app.getNetAddresss('plugin.love-speed-pool.frontend.wallet-site.index'),
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          this.setData({
            site: res.data.site,
            need_password: res.data.need_password,
            has_password: res.data.has_password
          });
        } else {
          app.tips(res.msg)
        }
      }
    });
  },
  async onSubmit() {
    let json = {site: this.data.site};
    if (this.data.need_password) {
      if(!this.data.has_password){
        wx.navigateTo({
          url: "/packageA/member/set_balance_password/set_balance_password" // 用户未设置支付密码，去设置密码
        });
        return
      } 
      let pass = await this.getPayKeyboardPassword();//弹出输入密码框
      json.password = pass;
    }
    app._postNetWork({
      url: app.getNetAddresss('plugin.love-speed-pool.frontend.wallet-site.edit-info'),
      data: json,
      success: (resdata) => {
        let res = resdata.data;
        app.tips(res.msg)
        if (res.result == 1) {
          wx.navigateBack({
            delta: 1
          })
        }
      },
    });
  },
  payKeyboardComplete(evt) {
    this.setData({
      popupSpecs: false
    });
    payKeyboardAsync.complete(evt.detail);
  },
  getPayKeyboardPassword() {
    this.setData({
      popupSpecs: true
    });
    return new Promise((resove, reject) => {
      payKeyboardAsync.addCompleteFn((pass) => {
        let urlStr = app.getNetAddresss("payment.password.check");
        urlStr += "&password=" + pass;
        app._getNetWork({
          url: urlStr,
          success: (resdata) => {
            var res = resdata.data;
            if (res.result == 1) {
              resove(pass);
            } else {
              wx.showModal({
                title: "提示",
                content: res.msg,
                success(restext) {
                  if (restext.confirm) {
                    if (res.data.code == 2002) {
                      // 用户未设置支付密码
                      // 去设置密码
                      wx.navigateTo({
                        url: "/packageA/member/set_balance_password/set_balance_password",
                      });
                    }
                  }
                },
              });
            }
          },
          fail: (res) => {
            console.log(res);
          },
        });
        
      });
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})